package com.example.tcp.register;

import com.example.common.constant.Constants;
import org.I0Itec.zkclient.ZkClient;


//关于zookeeper的内容   生成节点的方法
public class ZKit {

    private ZkClient zkClient;

    public ZKit(ZkClient zkClient) {
        this.zkClient = zkClient;
    }

    public void createRootNode(){
        boolean exists = zkClient.exists(Constants.ImCoreZkRoot);    //判断节点存在不
        if(!exists){
            zkClient.createPersistent(Constants.ImCoreZkRoot);
        }
        boolean tcpExists = zkClient.exists(Constants.ImCoreZkRoot +   //判断tcp是否存在
                Constants.ImCoreZkRootTcp);
        if(!tcpExists){
            zkClient.createPersistent(Constants.ImCoreZkRoot +
                    Constants.ImCoreZkRootTcp);
        }

        boolean webExists = zkClient.exists(Constants.ImCoreZkRoot +   //判断websocket是否存在
                Constants.ImCoreZkRootWeb);
        if(!webExists){
            zkClient.createPersistent(Constants.ImCoreZkRoot +
                    Constants.ImCoreZkRootWeb);
        }
    }

    //ip+port
    public void createNode(String path){     //创建子节点
        if(!zkClient.exists(path)){
//            zkClient.createEphemeral(path);
            zkClient.createPersistent(path);
        }
    }

}
